import PyInstaller.__main__
import os
import shutil

# 打包配置
APP_NAME = "TickTick"
MAIN_FILE = "main.py"
ICON_FILE = "app.ico"  # 确保图标文件存在
VERSION = "1.0.0"
COMPANY = "北京富通昌进出口有限公司"
COPYRIGHT = "© 2025 北京富通昌进出口有限公司版权所有"

# 清理之前的构建
if os.path.exists("dist"):
    shutil.rmtree("dist")
if os.path.exists("build"):
    shutil.rmtree("build")

# 打包命令参数
params = [
    MAIN_FILE,
    "--onefile",  # 打包成单个exe文件
    "--windowed",  # 不显示控制台窗口
    f"--name={APP_NAME}",
    f"--icon={ICON_FILE}",
    "--add-data=tasks.json;.",  # 包含数据文件
    "--add-data=app.ico;.",  # 包含图标文件
    "--noconfirm",  # 覆盖现有文件不提示
    "--clean"  # 清理临时文件
]

# 添加版本信息
if os.name == "nt":  # Windows系统
    version_info = f"""
# UTF-8
#
# For more details about fixed file info 'ffi' see:
# http://msdn.microsoft.com/en-us/library/ms646997.aspx
VSVersionInfo(
  ffi=FixedFileInfo(
    filevers=({VERSION.replace('.', ', ')}, 0),
    prodvers=({VERSION.replace('.', ', ')}, 0),
    mask=0x3f,
    flags=0x0,
    OS=0x40004,
    fileType=0x1,
    subtype=0x0,
    date=(0, 0)
  ),
  kids=[
    StringFileInfo(
      [
        StringTable(
          '040904B0',
          [
            StringStruct('CompanyName', '{COMPANY}'),
            StringStruct('FileDescription', '{APP_NAME}'),
            StringStruct('FileVersion', '{VERSION}'),
            StringStruct('InternalName', '{APP_NAME}'),
            StringStruct('LegalCopyright', '{COPYRIGHT}'),
            StringStruct('OriginalFilename', '{APP_NAME}.exe'),
            StringStruct('ProductName', '{APP_NAME}'),
            StringStruct('ProductVersion', '{VERSION}')
          ])
      ]),
    VarFileInfo([VarStruct('Translation', [1033, 1200])])
  ]
)
"""
    with open("version_info.txt", "w", encoding="utf-8") as f:
        f.write(version_info)
    params.append("--version-file=version_info.txt")

# 执行打包
PyInstaller.__main__.run(params)

print(f"打包完成，可执行文件在 dist 目录下: {APP_NAME}.exe")